package com.ifnk.portaljava.dapWeb.dapweb;
/**
 * <p>Title: 包头封装类</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2010</p>
 *
 * <p>Company: </p>
 *
 * @author WenSheng.Song.hlyt@gmail.com
 * @version 1.0
 */

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PacketHeader {
    private int len;
    private byte type;
    private byte subType;
    private byte protocol;
    private byte sc;
    private short device;
    private short channel;
    private int ts_s;
    private int ts_ns;
    private Log loger = LogFactory.getLog(PacketHeader.class);

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public byte getType() {
        return type;
    }

    public void setType(byte type) {
        this.type = type;
    }

    public byte getSubType() {
        return subType;
    }

    public void setSubType(byte subType) {
        this.subType = subType;
    }

    public byte getProtocol() {
        return protocol;
    }

    public void setProtocol(byte protocol) {
        this.protocol = protocol;
    }

    public byte getSc() {
        return sc;
    }

    public void setSc(byte sc) {
        this.sc = sc;
    }

    public short getDevice() {
        return device;
    }

    public void setDevice(short device) {
        this.device = device;
    }

    public short getChannel() {
        return channel;
    }

    public void setChannel(short channel) {
        this.channel = channel;
    }

    public int getTs_s() {
        return ts_s;
    }

    public void setTs_s(int ts_s) {
        this.ts_s = ts_s;
    }

    public int getTs_ns() {
        return ts_ns;
    }

    public void setTs_ns(int ts_ns) {
        this.ts_ns = ts_ns;
    }

    public PacketHeader() {
        super();
    }

    protected int Encode(byte[] pkt, int size)  {
        if (size < 20) {
            loger.error("dapweb Encode Function size < 20");
            throw new RuntimeException("size < 20");
        }

        pkt[0] = 0x7e;
        pkt[1] = 0x5a;

        pkt[4] = type;
        pkt[5] = subType;
        pkt[6] = protocol;

        this.EncodeShortToByte(pkt, 8, this.device);
        this.EncodeShortToByte(pkt, 10, this.channel);
        this.EncodeIntToByte(pkt, 12, this.ts_s);
        this.EncodeIntToByte(pkt, 16, this.ts_ns);

        return 20;
    }

    protected void EncodeIntToByte(byte[] b, int off, int n) {
        b[off] = (byte) (n >> 24 & 0xFF);
        b[off + 1] = (byte) ((n >> 16) & 0xFF);
        b[off + 2] = (byte) ((n >> 8) & 0xFF);
        b[off + 3] = (byte) (n & 0xFF);
    }

    private void EncodeShortToByte(byte b[], int off, short s) {
        b[off] = (byte) (s >> 8 & 0xFF);
        b[off + 1] = (byte) (s & 0xFF);
    }

    private int DecodeByteArrayToShort(byte[] b, int start) {
        return (b[start] << 8) + (b[start + 1] & 0xFF);
    }

    protected int DecodeByteArrayToInt(byte[] b, int start) {
        return ((b[start] & 0xFF) << 24) + ((b[start + 1] & 0xFF) << 16)
                + ((b[start + 2] & 0xFF) << 8) + (b[start + 3] & 0xFF);
    }

    protected void SetLength(byte[] packet, int len) {
        int length = 0;
        if (len % 4 == 0) {
            length = len / 4;
        } else {
            length = len / 4 + 1;
        }
        packet[2] = (byte) (length >> 8 & 0xFF);
        packet[3] = (byte) (length >> 0 & 0xFF);
        packet[7] = (byte) (length * 4 - len);
    }

    private int getPacketLength(byte[] packet) {
        return ((packet[2] << 8) + (packet[3] & 0xFF)) * 4 - (packet[7] & 0x3);
    }

    public void DecodeHeader(byte[] packet) {
        setLen(getPacketLength(packet));
        setType(packet[4]);
        setSubType(packet[5]);
        setProtocol(packet[6]);
        setSc(packet[7]);
        setDevice((short) DecodeByteArrayToShort(packet, 8));
        setChannel((short) DecodeByteArrayToShort(packet, 10));
        setTs_s(DecodeByteArrayToInt(packet, 12));
        setTs_ns(DecodeByteArrayToInt(packet, 16));
    }
}
